import pathToRegexp from 'path-to-regexp/index';
import {getEvacuate,postEvacuate,deleteEvacuate,putEvacuate} from 'services/app';
export default {
    namespace: 'evacuate',
    state: {
        record:{},
        pages:1,
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getEvacuate ({ payload }, { call, put }){
            const {data,total} = yield call(getEvacuate, payload);
            data && data.map((item,index) => {item.index = index + 1; });
            yield put({ type: 'save', payload: {getEvacuateData:data,total:total,pages:payload.pageNumber}});
        },
        //添加疏散场所
        *postEvacuate  ({ payload }, { call, put }){
            const data= yield call(postEvacuate, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,visible:false}});
            }
        },
        //删除疏散场所
        *deleteEvacuate ({ payload }, { call, put }){
            const data = yield call(deleteEvacuate, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,visible:false}});
            }
        },
        //修改疏散场所
        *putEvacuate ({ payload }, { call, put }){
            const data = yield call(putEvacuate, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,visible:false}});
            }
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
